<?php
/**
 * App_Ro
 *
 * @package    App
 * @subpackage App_Ro
 */

/**
 * フォロワーリソース
 *
 * @category   BEAR
 * @package    App
 * @subpackage App_Ro
 * @author     $Author: anonymous $ <anonymous@example.com>
 * @version    SVN: Release: $Id:$
 */
class App_Ro_User_Follower extends App_Ro
{

    /**
     * テーブル名
     *
     * @var string
     */
    protected $_table = App_Ro::TABLE_FOLLOWERS;

    /**
     * コンストラクタ
     */
    public function __construct(array $config)
    {
        parent::__construct($config);
    }

    /**
     * インジェクタ
     */
    public function onInject()
    {
        parent::onInject();
        $this->_query = BEAR::dependency('BEAR_Query');
        $this->_query->setConfig($this->_queryConfig);
    }

    /**
     * リソース作成
     *
     * @required user_id
     * @required follow_id
     *
     */
    public function onCreate($values)
    {
        $values['created_at'] = _BEAR_DATETIME;
        $result = $this->_query->insert($values);
        return $result;
    }

    /**
     * リソース読み込み
     *
     * @optional user_id
     * @optional follow_id
     */
    public function onRead($values)
    {
        if (isset($values['user_id'])) {
            $sql = 'SELECT followers.id,
    followers.created_at,
    followers.follow_id as user_id
FROM followers
WHERE followers.follow_id = user_id';
        } else {
            $sql = 'SELECT followers.id,
    followers.created_at,
    followers.follow_id as user_id
FROM followers
WHERE followers.user_id = user_id';
        }
        $result = $this->_query->select($sql, $values);
        return $result;
    }

    /**
     * リソース削除
     *
     * @required id
     */
    public function onDelete($values)
    {
        $values['deleted_at'] = _BEAR_DATETIME;
        $where = 'id = ' . $this->_query->quote($values['id'], 'integer');
        $result = $this->_query->update($values, $where);
        return $result;
    }

    /**
     * リンク
     *
     * @required id
     */
    function onLink($values)
    {
        $links = array('profile' => "user/profile?user_id={$values['id']}");
        return $links;
    }
}